[8주차/엠버] 워크북 제출합니다.#79
Open
haewonwon wants to merge 4 commits into
Open
Conversation
12234538
reviewed
May 25, 2026
| * - ESC 키 입력 시 자동으로 닫힘 | ||
| * - 사이드바가 열리면 body 스크롤을 막아 배경 콘텐츠가 스크롤되지 않음 | ||
| */ | ||
| function useSidebar() { |
There was a problem hiding this comment.
엠버! 사이드바 열고 닫는 코드가 진짜 깔끔해서 딱 봤을 때 이해가 잘 되는 것 같아요! 나중에 엠버코드를 참고해서 다시 한 번 만들어 봐야겠어요!
jjeunv
reviewed
May 25, 2026
Comment on lines
+1
to
+28
| import { useCallback, useRef } from 'react'; | ||
|
|
||
| /** | ||
| * 지정한 interval 동안 최대 한 번만 실행되는 스로틀된 함수를 반환함 | ||
| * - fn은 ref로 관리하여 interval이 바뀌지 않아도 항상 최신 콜백이 호출됨 | ||
| * - 언마운트 시 타이머를 clearTimeout으로 정리함 | ||
| */ | ||
| function useThrottle<T extends unknown[]>( | ||
| fn: (...args: T) => void, | ||
| interval: number, | ||
| ): (...args: T) => void { | ||
| const lastTimeRef = useRef(0); | ||
| const fnRef = useRef(fn); | ||
| fnRef.current = fn; | ||
|
|
||
| return useCallback( | ||
| (...args: T) => { | ||
| const now = Date.now(); | ||
| if (now - lastTimeRef.current >= interval) { | ||
| lastTimeRef.current = now; | ||
| fnRef.current(...args); | ||
| } | ||
| }, | ||
| [interval], | ||
| ); | ||
| } | ||
|
|
||
| export default useThrottle; |
Member
There was a problem hiding this comment.
저는 값(value)을 throttle하는 방식으로 구현했는데, 엠버는 함수(fn)를 직접 throttle하는 방식으로 구현했네요fnRef.current = fn 패턴으로 항상 최신 콜백을 참조하는 부분이 인상적입니다 ! !
현재 구현은 interval 안에 들어온 마지막 이벤트가 완전히 무시되는 것 같은데, 저는 setTimeout으로 남은 시간 후에 trailing 실행을 해줬습니다. 사용 목적에 따라 다를 것 같지만 만일 마지막 이벤트도 반영되어야 한다면 trailing 처리를 고려해보면 좋을 것 같아요!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
✅ 워크북 체크리스트
✅ 컨벤션 체크리스트
📌 주안점